package com.haozi.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haozi.reggie.common.CustomException;
import com.haozi.reggie.entity.Category;
import com.haozi.reggie.entity.Dish;
import com.haozi.reggie.entity.Setmeal;
import com.haozi.reggie.mapper.CategoryMapper;
import com.haozi.reggie.service.CategoryService;
import com.haozi.reggie.service.DishService;
import com.haozi.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        //构造查询条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(queryWrapper);
        if(count1 > 0) {
            //如果查询结果大于0  则表示关联了菜品
            throw  new CustomException("关联了菜品,不能删除");
        }
        //构造查询条件
        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(queryWrapper1);
        if(count2 > 0) {
            //如果查询结果大于0  则表示关联了套餐
            throw  new CustomException("关联了套餐,不能删除");
        }
        //执行删除操作
        super.removeById(id);

    }
}
